import sys

from framework.common.pt_logger import logger
from util import eventWrapper

overridden_parameters = {
    "enableNotification": False
}

spider_module_name_map = {  # JimmyMo: 这里的Key是在EventBridge中配的消息模板中来的
    "CoreProductsBestSellers": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsBestSellers"}},
    "CoreProductsMostWishedFor": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsMostWishedFor"}},
    "CoreProductsGiftIdeas": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsGiftIdeas"}},
    "CoreProductsMoversAndShakers": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsMoversAndShakers"}},
    "CoreProductsNewReleases": {"ModuleName": "spiders.coreProducts_spider.coreProducts_spider", "Parameters": {"spider_name": "CoreProductsNewReleases"}},
    "AsinSpiderInitiator": {"ModuleName": "spiders.asin_spider.asin_spider", "Parameters": {"spider_name": "AsinSpiderInitiator"}},
    "AsinDetailPage": {"ModuleName": "spiders.asin_spider.asin_spider", "Parameters": {"spider_name": "AsinDetailPage"}},
    "AsinCustomerReview": {"ModuleName": "spiders.asin_spider.asin_spider", "Parameters": {"spider_name": "AsinCustomerReview"}},
    "AsinQuestionAndAnswer": {"ModuleName": "spiders.asin_spider.asin_spider", "Parameters": {"spider_name": "AsinQuestionAndAnswer"}},
}

# SpiderInitiator 从CloudWatch接受触发事件，往队列中放入初始化url
def processRequest(req, event=None, context=None):
    """ event format reference: (from cloud watch)
    {
        "id": "40a936c0-9489-3a7d-ca57-798ff695d809",
        "time": "2021-05-10T04:36:00Z",
        "source": "aws.events",
        "detail-type": "Scheduled Event",
        "detail": {
            "state": "STARTED",
            "metadata": {
                "SpiderName": "CoreProductsBestSellers"
            }
        }
    }
    """
    sys.path.append('jobs/amazon')
    logger.info("started")
    spiderName = req["metadata"]["SpiderName"]  # 这个是在template中定义的
    payload = req.get("payload")
    logger.info("input spider name: %s" % spiderName)
    spider_module_name = spider_module_name_map[spiderName]["ModuleName"]
    __import__(spider_module_name)
    spider_lib = sys.modules[spider_module_name]
    batch_id = event["time"].split("T")[0]
    logger.info("batch_id: %s" % batch_id)
    spider_lib.batch_id = batch_id
    parameters = list(spider_module_name_map[spiderName]["Parameters"].items())
    for item in parameters:
        spider_lib.__dict__[item[0]] = item[1]
        logger.info("Parameter pair key: %s, value: %s" % (item[0], item[1]))
    spider_lib.start_initiator(payload)


globals()['eventWrapper'].global_parameters = {**globals()['eventWrapper'].global_parameters, **overridden_parameters}
globals()['eventWrapper'].processRequest = processRequest
globals()['handler'] = eventWrapper.handler